package top.deepdesigner.controller.systemManage;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsAdmin;
import top.deepdesigner.service.DtsAdminService;
import top.deepdesigner.util.AdminResponseCode;
import top.deepdesigner.util.AdminResponseUtil;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.AdminListVO;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/21 星期三 15:55:16 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/admin")
public class AdminController {
  @Autowired
  private DtsAdminService dtsAdminService;

  //权限
  //系统:模块:功能
  @RequiresPermissionsDesc(menu = {"系统管理", "管理员"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:admin:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");
    String username = (String) params.get("username");

    //角色列表VO
    AdminListVO adminListVO = new AdminListVO();
    //获取分页用户角色数量
    Long count = dtsAdminService.getAdminCount();
    //获取分页用户角色列表
    List<DtsAdmin> dtsAdmins = dtsAdminService
        .getAdminList(page, limit, sort, order, username)
        .stream()
        .map(item -> item.setPassword(""))
        .collect(Collectors.toList());
    adminListVO.setTotal(count);
    adminListVO.setItems(dtsAdmins);
    return ResponseUtil.ok(adminListVO);
  }

  @RequiresPermissionsDesc(menu = {"系统管理", "管理员"}, button = "添加")
  @RequiresPermissions(value = {"admin:admin:create"})
  @PostMapping("/create")
  @Transactional
  public Object create(@RequestBody DtsAdmin dtsAdmin) {
    DtsAdmin dtsAdminRes = dtsAdminService.createAdmin(dtsAdmin);
    if (dtsAdminRes == null) {
      return AdminResponseUtil.fail(AdminResponseCode.ADMIN_NAME_EXIST);
    }
    return ResponseUtil.ok(dtsAdminRes);
  }

  @RequiresPermissionsDesc(menu = {"系统管理", "管理员"}, button = "编辑")
  @RequiresPermissions(value = {"admin:admin:update"})
  @PostMapping("/update")
  @Transactional
  public Object update(@RequestBody DtsAdmin dtsAdmin) {
    DtsAdmin dtsAdminRes = dtsAdminService.updateAdmin(dtsAdmin);
    return ResponseUtil.ok(dtsAdminRes);
  }

  @RequiresPermissionsDesc(menu = {"系统管理", "管理员"}, button = "删除")
  @RequiresPermissions(value = {"admin:admin:delete"})
  @PostMapping("/delete")
  @Transactional
  public Object delete(@RequestBody DtsAdmin dtsAdmin) {
    dtsAdminService.deleteAdmin(dtsAdmin.getId());
    return ResponseUtil.ok();
  }
}
